package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOPeration{
    @Override
    public void work(BookList booklist) {
        System.out.println("删除图书！");
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入您要删除图书的书名：");
        int currentSize = booklist.getUsedSize();
        
        int index = -1;
        String name = scanner.nextLine();
        int i = 0;
        for (i = 0; i < currentSize; i++) {
            Book tmp = booklist.getBook(i);
            if(tmp.getName().equals(name)){
                index = i;
                break;
            }
        }
        if(i >= currentSize){
            System.out.println("存在这本书，不能重复添加！");
            return;
        }

        for (int j = index; j < currentSize; j++) {
            Book book = booklist.getBook(j+1);
            booklist.setBook(book,j);
            
        }

        booklist.setBook(null,currentSize - 1);
        booklist.setUsedSize(currentSize - 1);
    }
}
